package com.chuangjiangx.member.basic.ddd.query;

import com.chuangjiangx.commons.CJBeanUtils;
import com.chuangjiangx.member.basic.ddd.dal.mapper.MerchantUserInfoDalMapper;
import com.chuangjiangx.member.basic.ddd.dal.dto.MerchantComponent;
import com.chuangjiangx.member.basic.ddd.dal.dto.MerchantUser;
import com.chuangjiangx.member.basic.dao.mapper.InMemberMapper;
import com.chuangjiangx.member.basic.dao.model.InMember;
import com.chuangjiangx.member.basic.ddd.dal.dto.MerchantUserInfo;
import com.chuangjiangx.member.basic.ddd.dal.dto.StoreUserInfo;
import com.chuangjiangx.member.basic.ddd.query.dto.UserInfoDTO;
import com.chuangjiangx.member.basic.ddd.query.dto.MerchantUserDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 商户查询相关
 * Created by YangKuo on 2018/1/16.
 */
@Service
public class MerchantUserInfoQuery {

    @Autowired
    private MerchantUserInfoDalMapper merchantUserInfoDalMapper;

    @Autowired
    private InMemberMapper inMemberMapper;

    public List<MerchantUserDTO> searchMerchantUserByMerchantId(Long merchantId) throws Exception {
        Assert.notNull(merchantId, "商户id不能为空");

        List<MerchantUser> list = merchantUserInfoDalMapper.selectMerchantUserListByMerchantId(merchantId);

        List<MerchantUserDTO> respList = null;
        if (list == null) {
            throw new Exception("当前商户下，商户用户不存在！");
        }
        respList = CJBeanUtils.convertCollection(list, MerchantUserDTO.class, null);
        return respList;
    }


    public Long getDefaultMerchantUserId(Long merchantId) {

        List<Long> list = merchantUserInfoDalMapper.selectDefaultMerchantUserId(merchantId);

        if (list.isEmpty()) {
            return null;
        }

        return list.get(0);
    }

    /**
     * 根据会员Id查询 merchatUserId
     * @param memberId
     * @return
     */
    public Long getDefaultMerchantUserIdWithMemberId(Long memberId) {

        InMember member = inMemberMapper.selectByPrimaryKey(memberId);


        return getDefaultMerchantUserId(member.getMerchantId());
    }

    /**
     * 查询用户信息
     * @param merchantUserId
     * @return
     */
    public UserInfoDTO queryUserInfo(Long merchantUserId){
        MerchantUserInfo merchantUserInfo = merchantUserInfoDalMapper.selectMerchantUserInfo(merchantUserId);
        if(merchantUserId == null){
            return null;
        }
        UserInfoDTO dto = new UserInfoDTO();
        BeanUtils.copyProperties(merchantUserInfo,dto);
        dto.setUserType(0);
        dto.setRealName(merchantUserInfo.getMerchantContact());
        if(null != merchantUserInfo.getStoreUserId()){
            StoreUserInfo storeUserInfo = merchantUserInfoDalMapper.selectStoreUserInfo(merchantUserInfo.getStoreUserId());
            if(null != storeUserInfo){
                BeanUtils.copyProperties(storeUserInfo,dto);
            }
        }
        return dto;
    }

    public MerchantUserDTO queryMerchantUserInfoByUsername(String username) {
        MerchantUser merchantUser = merchantUserInfoDalMapper.selectMerchantUserByUsername(username);
        if (merchantUser == null) {
            return null;
        }

        MerchantUserDTO dto = new MerchantUserDTO();
        BeanUtils.copyProperties(merchantUser, dto);

        return dto;
    }

    public String queryUserPassword(Long merchantUserId){
        if(null == merchantUserId){
            return null;
        }
        return merchantUserInfoDalMapper.selectMerchantUserPassword(merchantUserId);
    }

    /**
     * 查询商户用户权限列表
     * @param merchantUserId
     * @param plat 1.商户平台 3: APP
     * @return
     */
    public List<MerchantComponent> queryMerchantComponentList(Long merchantUserId, Integer plat){
        return merchantUserInfoDalMapper.selectMerchantComponent(merchantUserId,plat);
    }

}
